package mya_dc.shared_classes;

import java.io.File;
import java.util.LinkedList;

import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

/**
 * a class thats saves all the errors that will return from the slaves
 *
 * @author      Adam Levi
 * <br>			MYA
 */
public class CompilationErrors extends LinkedList<MyaCompilationError> implements TableModel
{
	private static final long serialVersionUID = 1L;
	
	/**
	 * default constructor
	 */
	public CompilationErrors()
	{
		super();
	}
	
	/**
	 * A 'pretty' printing to string
	 */
	@Override public String toString()
	{
		if (this.size() == 0)
			return "No errors";
		String ret = "";
		for (MyaCompilationError error : this)
			ret += (error.toString() + "\n");
		return ret;
	}

	@Override
	public void addTableModelListener(TableModelListener l) {
		// TODO Auto-generated method stub
		
	}

	/*
	 * (non-Javadoc)
	 * The following is an the implementation of TableModel 
	 */
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return getValueAt(0, columnIndex).getClass();
	}

	@Override
	public int getColumnCount() {
		return CompilationErrors.ColumnCount;
	}

	@Override
	public String getColumnName(int columnIndex) {
		return CompilationErrors.ColumnName[columnIndex];
	}

	@Override
	public int getRowCount() {
		return this.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return this.get(rowIndex,columnIndex);
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	@Override
	public void removeTableModelListener(TableModelListener l) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		// TODO Auto-generated method stub
		
	}
	
	
	/**
	 * This method take the tree and 'looks' at it as a table of 
	 * treeSize * 4. where:
	 * <br> column 0 is the error number
	 * <br> column 1 is the filename
	 * <br> column 2 is the line number
	 * <br> column 3 is the error string
	 * @return the object located at i,j
	 */
	private Object get(int i, int j)
	{
		if (j == 0)
			return i+1;
		if (i >= this.size() || j >= CompilationErrors.ColumnCount)
			return null;
		int k = 0;
		for (MyaCompilationError er : this)
		{
			if (k++ == i)
			{
				switch(j)
				{
				case 1: return new File(er.m_strFileName).getName();
				case 2: return er.m_strLineNum;
				case 3: return er.m_strError;
				}
			}
		}
		return null;
	}
	
	private static String[] ColumnName = {"", "Resource", "", "Message"};
	private static final int ColumnCount = 4;

}